/**
 * 全员营销活动|部门排行 cyy
 */
var pageIndex = 1;
var pageSize = 10;
var tabTime = 1;
var loadValid = true;
var flagIndex = 1;
var zeroFlag = false;
var scoreRanking = 1;
var appendFlag = false;
var firsReplacetHtml = false;
var departmentRanking = {
		token : null,
		init : function(accessToken){
			departmentRanking.token = accessToken;
			return departmentRanking;
		},
		// 当前部门排行
		currentDepRanking : function(tabTime) {
			var url = departmentRanking.getPath()+"/aem/client/currentDepRanking";
			console.log("---->"+departmentRanking.token);
	        $.post(url, {
	        	"token":departmentRanking.token,
	        	"tabTime":tabTime,
	        }, function (data) {
	            if (data.status == "200" || data.status == "201") {
	            	var departmentInfo = data.departmentInfo;
	            	var rankingData = data.data;
	            	if(StringEmpty(departmentInfo)){
	            		if(StringEmpty(departmentInfo.parentDepartmentName)){
	            			$('#staffName').html(departmentInfo.currentDepartmentName);
			            	$('#staffCompany').html(departmentInfo.parentDepartmentName);
	            		}else{
	            			$('#staffName').html(departmentInfo.currentDepartmentName);
	            		}
	            	}
	            	
	            	if(StringEmpty(rankingData)){
	            		$('#depAvgScore').html(rankingData.depAvgScore);
	            		$('#scoreRanking').html(rankingData.scoreRanking);
	            	}else{
	            		$('#depAvgScore').html('0');
	            		$('#scoreRanking').html('0');
	            	}
	            } else {
	            	//跳转到登录页
	            	/*window.location.href="";*/
	            	console.log(data);
	            }
	        });
		},
		// 部门排行
		departmentRanking : function(pageIndex,tabTime){
			var url = departmentRanking.getPath()+"/aem/client/departmentRanking";
			console.log("---->"+departmentRanking.token);
	        $.post(url, {
	        	"token":departmentRanking.token,
	        	"index":pageIndex,
	        	"size":10,
	        	"tabTime":tabTime,
	        	"flagIndex":flagIndex,
	        }, function (data) {
	            if (data.status == "200" || data.status == "10000") {
	            	var rankingData = data.data;
	            	var heroHtml="";
	            	var rankingHtml="";
	            	if(StringEmpty(rankingData)){
	            		for(var i = 0;i<rankingData.length;i++){
	            			console.log("scoreRanking-------------->"+scoreRanking);
		            		if(scoreRanking < 4){
		            			heroHtml += '<div data-v-f534367c="" class="department-rank">';
		            			if(scoreRanking == 1){
		            				heroHtml += '<img data-v-f534367c="" src="" class="number">';
		            			}else if(scoreRanking == 2){
		            				heroHtml += '<img data-v-f534367c="" src="" class="number">';
		            			}else{
		            				heroHtml += '<img data-v-f534367c="" src="" class="number">';
		            			}
		            			heroHtml += '<div data-v-f534367c="" class="department-icon-wrap">';
		            			heroHtml += '<img data-v-f534367c="" src="" class="department-icon"> ';
		            			if(scoreRanking == 1){
		            		    	heroHtml += '<img data-v-f534367c="" src="" class="department-rank-icon"></div>';
		            		    }else if(scoreRanking == 2){
		            		    	heroHtml += '<img data-v-f534367c="" src="" class="department-rank-icon"></div>';
		            		    }else{
		            		    	heroHtml += '<img data-v-f534367c="" src="" class="department-rank-icon"></div>';
		            		    }
		            			heroHtml += '<div data-v-f534367c="" class="info">';
		            			
		            			if(StringEmpty(rankingData[i].parentDepartmentName)){
		            				heroHtml += '<p data-v-f534367c="" class="depart-name">'+rankingData[i].currentDepartmentName+'</p>';
			            			heroHtml += '<p data-v-f534367c="" class="depart-address">'+rankingData[i].parentDepartmentName+'</p></div>';
			            		}else{
			            			heroHtml += '<p data-v-f534367c="" class="depart-name">'+rankingData[i].currentDepartmentName+'</p></div>';
			            		}
		            			heroHtml += '<p data-v-f534367c="" class="depart-score">+<span data-v-f534367c="">'+rankingData[i].depAvgScore+'</span>积分/人</p></div>';
		            		}else{
		            			rankingHtml += '<li><div class="head-bg"><span style="font-size: 0.32rem;margin-left: 4px;" class="number">'+scoreRanking+'</span>';
			            		rankingHtml += '<img style="margin-left: 3px;" src="" class="head">';
			            		if(StringEmpty(rankingData[i].parentDepartmentName)){
			            			rankingHtml += '<span class="name" style="left: 1.60rem;">'+rankingData[i].currentDepartmentName +'</span>';
			            			rankingHtml += '<span class="name" style="left: 1.60rem;margin-top:24px ;font-size:.24rem;color: #666;">'+rankingData[i].parentDepartmentName+'</span>';
			            		}else{
			            			rankingHtml += '<span class="name" style="left: 1.60rem;margin-top:20px;position: inherit">'+rankingData[i].currentDepartmentName+'</span>';
			            		}
			            		rankingHtml += '<div class="integral" style="right: -0.8rem;">+<span>'+rankingData[i].depAvgScore+'</span>积分/人</div></div></li>';

		            		}
		            		scoreRanking++;
		            		appendFlag = true;
	            		}
	            	}
	            	console.log("firsReplacetHtml--------->"+firsReplacetHtml);
	            	if(appendFlag && firsReplacetHtml){
	            		$('.department-list').append(heroHtml);
		            	$('.rank-list').append(rankingHtml);
	            	}else{
	            		firsReplacetHtml = true;
	            		$('.department-list').html(heroHtml);
		            	$('.rank-list').html(rankingHtml);
	            	}
	            	if(data.status == "10000" ){
	            		zeroFlag = true;
            			flagIndex++;
            			departmentRanking.departmentRanking(2,tabTime);  
            		}
	            }else if(data.status == "201"){
	            	if(pageIndex == 1){
	            		$('.department-list').html("");
		            	$('.rank-list').html("");
	            	}
	            	loadValid = false ; 
	            }else{
	            	//跳转到登录页
	            	/*window.location.href="";*/
	            	console.log(data);
	            }
	        });
		},
		
		getPath : function(){ 
		     return $('#servicePath').attr("servicePath");
		},
		parseUrl : function(url) {
	        var a =  document.createElement('a');
	        a.href = url;
	        return {
	            source: url,
	            protocol: a.protocol.replace(':',''),
	            host: a.hostname,
	            port: a.port,
	            query: a.search,
	            params: (function(){
	                var ret = {},
	                    seg = a.search.replace(/^\?/,'').split('&'),
	                    len = seg.length, i = 0, s;
	                for (;i<len;i++) {
	                    if (!seg[i]) { continue; }
	                    s = seg[i].split('=');
	                    ret[s[0]] = s[1];
	                }
	                return ret;
	            })(),
	            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
	            hash: a.hash.replace('#',''),
	            path: a.pathname.replace(/^([^\/])/,'/$1'),
	            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
	            segments: a.pathname.replace(/^\//,'').split('/')
	        };
	    }
		
}

// 昨天
$('#one').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	flagIndex = 1;
	tabTime = 1;
	scoreRanking=1;
	appendFlag = false;
	firsReplacetHtml = false;
	$('.mint-tab-item').removeClass("is-selected");
	$('#one').addClass("is-selected");
	$('.departmentRankingTip').html("近1天排名");
	$('.department-list').html('');
	$('.rank-list').html('');
	departmentRanking.currentDepRanking(tabTime);
	departmentRanking.departmentRanking(pageIndex,tabTime);
});
//近七天
$('#senven').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	flagIndex = 1;
	tabTime = 7;
	scoreRanking=1;
	appendFlag = false;
	firsReplacetHtml = false;
	$('.mint-tab-item').removeClass("is-selected");
	$('#senven').addClass("is-selected");
	$('.departmentRankingTip').html("近7天排名");
	$('.department-list').html('');
	$('.rank-list').html('');
	departmentRanking.currentDepRanking(tabTime);
	departmentRanking.departmentRanking(pageIndex,tabTime);  
});
//近30天
$('#month').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	flagIndex = 1;
	tabTime = 30;
	scoreRanking=1;
	appendFlag = false;
	firsReplacetHtml = false;
	$('.mint-tab-item').removeClass("is-selected");
	$('#month').addClass("is-selected");
	$('.departmentRankingTip').html("近30天排名");
	$('.department-list').html('');
	$('.rank-list').html('');
	departmentRanking.currentDepRanking(tabTime);
	departmentRanking.departmentRanking(pageIndex,tabTime); 
});
//总榜
$('#all').click(function(){
	loadValid = true;
	zeroFlag = false;
	pageIndex = 1;
	flagIndex = 1;
	tabTime = "";
	scoreRanking=1;
	appendFlag = false;
	firsReplacetHtml = false;
	$('.mint-tab-item').removeClass("is-selected");
	$('#all').addClass("is-selected");
	$('.departmentRankingTip').html("总排名");
	$('.department-list').html('');
	$('.rank-list').html('');
	departmentRanking.currentDepRanking(tabTime);
	departmentRanking.departmentRanking(pageIndex,tabTime);  
});

//监听是否滑到底部
window.addEventListener('scroll', throttle(loadData, 500, {leading:false}), false);

//加载更多数据
function loadData(){
	if($(window).scrollTop() + $(window).height() >= $(document).height()&&loadValid){
		if(zeroFlag){
			flagIndex++;
		}
		pageIndex++;
		departmentRanking.departmentRanking(pageIndex,tabTime); 
	}
}
//频率控制函数
function throttle(fn,delay,options) {
     var wait=false;
     if (!options) options = {};
     return function(){
         var that = this,args=arguments;
         if(!wait){
             if (!(options.leading === false)){
                 fn.apply(that,args);
             }
             wait=true;
             setTimeout(function () {
                 if (!(options.trailing === false)){
                     fn.apply(that,args);
                 }
                 wait=false;
             },delay);
         }
     }
 }
